home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8174 / 8174.xpi / chrome / antbar.jar / content / grabber / queryobserver.js < prev   
Text File  |  2009-12-30  |  6KB  |  185 lines

  1. // 
  2. //  queryobserver.js
  3. //  firefox
  4. //  
  5. //  Created by Zak on 2008-06-09.
  6. //  Copyright 2008-2009 Ant.com. All rights reserved.
  7. // 
  8.  
  9. /**
  10.  * Class: Observer called when firefox download something
  11.  * @constructor     No parameters
  12.  */
  13. var AntQueryObserver = function ()
  14. {
  15.     this.unvalidContentTypes = {
  16.         "application/x-shockwave-flash":1,
  17.     };
  18.  
  19.     this.possibleContentTypes = {
  20.         "flv-application/octet-stream":1,
  21.         "application/octet-stream":1,
  22.         "text/plain":1,
  23.         "text/html":1
  24.     };
  25.    
  26.     
  27.     this.isValidContentLength = function (cl)
  28.     {
  29.         if (isNaN(cl) || cl < 500000)
  30.             return false;
  31.         return true;
  32.     };
  33.     
  34.  
  35.     this.regValidContentType = /video/,
  36.  
  37.     this.isValidContentType = function (ct)
  38.     {
  39.         if (ct.match(this.regValidContentType))
  40.             return true;
  41.         return false;
  42.     };
  43.  
  44.     /**
  45.      * Is this URL a junk URL ? From a ads for exemple
  46.      * @param url           The URL to check
  47.      * @return boolean
  48.      */
  49.     this.regForceNoJunk = /advert[0-9]*\.pornhub\.com/,
  50.     this.regJunk = /ads|advert|banner|doubleclick|visiblemeasures\.com|video-stats\.video\.google\.com|noflv|(info|widget)\.break\.com|diggthis/,
  51.  
  52.     this.isJunkFlv = function (request)
  53.     {
  54.         if (this.unvalidContentTypes[request.contentType])
  55.             return true;
  56.  
  57.         if (request.name.match(this.regForceNoJunk))
  58.             return false;
  59.  
  60.         if (request.name.match(this.regJunk))
  61.             return true;
  62.  
  63.         return false;
  64.     };
  65.     
  66.     /**
  67.      * Is this URL a valid URL ? Assuming the content type as been checked first
  68.      * @param url           The URL to check
  69.      * @return boolean
  70.      */
  71.     this.regValidFlv = /flv|video|movie|film|clip|f4v|vod|mp4/,
  72.  
  73.     this.isValidFlv = function (url)
  74.     {
  75.         return url.match(this.regValidFlv);
  76.     };
  77.     
  78.     /**
  79.      * Is this URL the result of a seek in a player ?
  80.      * @param url           The URL to check
  81.      * @return boolean
  82.      */
  83.     // &fs=XXX => seek dailymotion
  84.     this.regSeekUrl = /[?&](st(art)?|begin|fs)=([1-9][0-9]*|0+[1-9]+)/i,
  85.  
  86.     this.isSeekUrl = function (url)
  87.     {
  88.         return url.match(this.regSeekUrl);
  89.     };
  90.  
  91.     /**
  92.      * Check if the given request is a valid flv file
  93.      * @param request       The request object to authentify
  94.      * @return boolean      True if the request is for a valid flv file, false instead
  95.      */
  96.     this.isFlv = function (request)
  97.     {
  98.         if (this.isSeekUrl(request.name))
  99.         {
  100.             AntLib.toLog(request.name + " Rejected => Seek URL");
  101.             return false;
  102.         }
  103.  
  104.         if (this.isJunkFlv(request))
  105.     {
  106.             AntLib.toLog(request.name + " Rejected => Junk");
  107.         return false;
  108.     }
  109.  
  110.         if (this.isValidContentType(request.contentType))
  111.         {
  112.             AntLib.toLog(request.name + " Accepted => True Content & noJunk");
  113.             return true;
  114.         }
  115.         if (this.isValidContentLength(request.contentLength) &&
  116.             this.possibleContentTypes[request.contentType])
  117.         {
  118.             AntLib.toLog(request.name + " Accepted =>  validContentLength & possibleContentType & noJunk");
  119.             return true;
  120.         }
  121.  
  122.         AntLib.toLog("Rejected => " + " url:" + request.name + " content-type:" + request.contentType);
  123.         return false;
  124.     };
  125.     
  126.     this.getDocumentByRequest = function (request)
  127.     {
  128.         if (request.notificationCallbacks)
  129.         {
  130.             if (!(request.notificationCallbacks instanceof Components.interfaces.nsIDocShell))
  131.                 return null;
  132.             try {
  133.                 var interfaceRequestor = AntLib.QI(request.notificationCallbacks, AntLib.CI("nsIInterfaceRequestor"));
  134.                 var win = AntLib.GI(interfaceRequestor, AntLib.CI("nsIDOMWindow"));
  135.                 var currentDoc = win.document;
  136.                 var rootDoc = currentDoc;
  137.  
  138.                 while (rootDoc.defaultView.frameElement)
  139.                     rootDoc = rootDoc.defaultView.frameElement.ownerDocument;
  140.  
  141.                 return rootDoc;
  142.             }
  143.             catch (err) {
  144.                 return null;
  145.             }
  146.         }
  147.         return null;
  148.     };
  149.  
  150.     /**
  151.      * The function called by firefox, entry point of this class
  152.      * @param request       The object containing the request params
  153.      * @param topic         The specific event that trigger the function
  154.      * @param data          the request data (seems to be empty)
  155.      */
  156.     this.observe = function (request, topic, data)
  157.     {
  158.         var request = request.QueryInterface(Components.interfaces["nsIHttpChannel"]);
  159.         var doc = this.getDocumentByRequest(request);
  160. //        AntLib.toLog("Request.name = " + request.name + " do isNULL = " + (doc == null));
  161.  
  162.         if (doc == null)
  163.             return;
  164.  
  165.         if (this.isFlv(request))
  166.         {
  167.             var name = AntLib.safeGet(doc, "title");
  168.             AntLib.toLog("FLV NAME: " + name);
  169.  
  170.             // FIXME: add score
  171.             var flvLink = new AntFlvLink({origin: AntLib.getSiteName(doc.location),
  172.                                           url: request.name,
  173.                                           name: name,
  174.                                           doc: doc,
  175.                                           type: request.contentType,
  176.                                           length: request.contentLength,
  177.                                           charset: request.contentCharset,
  178.                                           size: request.contentLength});
  179.  
  180.             AntGrabber.foundFlvLink(flvLink);
  181.         }
  182.     };
  183. }
  184.  
  185.